﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml;

namespace ConsoleApp1
{
    class FxConfigConfigurationProvider : FileConfigurationProvider
    {
        public FxConfigConfigurationProvider(FileConfigurationSource source) : base(source)
        {
        }

        public override void Load(Stream stream)
        {
            //var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(stream);

            var nodeConnStrs = xmlDoc.SelectNodes("/configuration/connectionStrings/add");
            foreach (var nodeConnStr in nodeConnStrs.Cast<XmlNode>())
            {
                string name = nodeConnStr.Attributes["name"].Value;
                string connectionString = nodeConnStr.Attributes["connectionString"].Value;
                Data[$"{name}:connectionString"] = connectionString;

                var providerNameProp = nodeConnStr.Attributes["providerName"];
                if (providerNameProp != null)
                {
                    //providerName is optional
                    string providerName = providerNameProp.Value;
                    Data[$"{name}:providerName"] = providerName;
                }
            }
        }
    }
}
